package cn.baiyang.rocketmq.example.consumer;

import cn.baiyang.rocketmq.spring.annotation.RocketMQMessageListener;
import cn.baiyang.rocketmq.spring.annotation.SelectorType;
import cn.baiyang.rocketmq.spring.core.RocketMQListener;
import cn.baiyang.rocketmq.spring.core.RocketMQListenerHelper;

import java.util.concurrent.atomic.AtomicLong;

import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author hongzhu
 * @version V1.0
 * @since 2023-07-26 16:17
 */
@Slf4j
@Service
@RocketMQMessageListener(
    topic = "${business.topic}",
    consumerGroup = "${business.consumer-group}",
    selectorType = SelectorType.TAG,
    selectorExpression = "${business.tag}"
)
public class ListenerExample extends RocketMQListenerHelper implements RocketMQListener {

    private final AtomicLong counter = new AtomicLong(0);

    @Override
    public ConsumeResult consume(MessageView messageView) {
        log.info("{}. Message: {}; --> body: {}", counter.incrementAndGet(), messageView, getBodyAsString(messageView));
        //TODO Business Logic Here
        return ConsumeResult.SUCCESS;
    }

}
